#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
SPE to TIF 宏代码生成器 - 演示版本
"""

import os
import glob

def generate_macro_code(input_path, output_path):
    """
    生成ImageJ宏代码
    
    Args:
        input_path (str): 包含.spe文件的文件夹路径
        output_path (str): TIF文件输出文件夹路径
    
    Returns:
        str: 生成的宏代码
    """
    # 查找所有.spe文件
    spe_files = glob.glob(os.path.join(input_path, "*.spe"))
    
    if not spe_files:
        return "未找到.spe文件"
    
    # 生成宏代码
    macro_code = []
    for spe_file in spe_files:
        filename = os.path.basename(spe_file)
        name_without_ext = os.path.splitext(filename)[0]
        
        # 生成相对路径，并转换为正斜杠
        rel_input_path = os.path.relpath(spe_file, os.getcwd()).replace('\\', '/')
        rel_output_path = os.path.join(output_path, f"{name_without_ext}.tif").replace('\\', '/')
        
        macro_code.append(f'open("{rel_input_path}");')
        macro_code.append(f'saveAs("Tiff", "{rel_output_path}");')
        macro_code.append('close();')
        macro_code.append("")  # 空行分隔
    
    return "\n".join(macro_code)

def main():
    """主函数 - 演示功能"""
    print("SPE to TIF 宏代码生成器 - 演示版本")
    print("=" * 50)
    
    # 示例路径
    input_path = "A"  # 输入路径
    output_path = "B"  # 输出路径
    
    print(f"输入路径: {input_path}")
    print(f"输出路径: {output_path}")
    print()
    
    # 生成宏代码
    macro_code = generate_macro_code(input_path, output_path)
    
    print("生成的宏代码:")
    print("-" * 30)
    print(macro_code)
    
    # 保存到文件
    output_file = "generated_macro.ijm"
    with open(output_file, 'w', encoding='utf-8') as f:
        f.write(macro_code)
    
    print(f"\n宏代码已保存到: {output_file}")

if __name__ == "__main__":
    main() 